package com.xja.controller;/**
 * @className: AdminController
 * @description: TODO 类描述
 * @author: wkx
 * @date: 2022/4/17
 **/
import com.xja.pojo.*;
import com.xja.service.ArticleService;
import com.xja.service.BlogService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.*;

import com.xja.pojo.Article;
import com.xja.pojo.BlogUsers;
import com.xja.service.UserService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : AdminController  //类名

 * @Author : Lenovo //作者
 * @Date: 2022/4/17  10:52
 */
@Controller
@RequestMapping("blog")
public class BlogController {
    private Logger logger= LoggerFactory.getLogger(BlogController.class);
    @Reference(url = "dubbo://localhost:20883")
    private BlogService blogService;
    @Reference(url = "dubbo://localhost:20881")
    private UserService userService;
    @Reference(url ="dubbo://localhost:20883" )
    private ArticleService articleService;
    //获取首页信息
    @RequestMapping("getIndex")
    public String getHotestArticle(Model model){
        logger.info("首页控制器");
//        List<ContentType> contentTypeList=blogService.getContentType();  //首页分类写固定了
        List<Article> hotestArticleList = blogService.getHotestArticle();
        Map<Integer,String> indexmap=new HashMap<>();
        for (Article a:hotestArticleList) {
            BlogUsers blogUsers = userService.selectUserById(a.getBloguid());
            indexmap.put(a.getAid(), blogUsers.getUname());
        }
        model.addAttribute("indexmap",indexmap);
        model.addAttribute("hotestArticleList",hotestArticleList);
        List<BlogUsers> blogUsersList=blogService.getHotestUser();
        model.addAttribute("blogUsersList",blogUsersList);
        return "index";
    }
    @RequestMapping("/getCourseInfoList")
    public String getCourseInfoList(Model model,@RequestParam(value = "counts",defaultValue = "0") Integer counts){
        counts = counts+2;
        CourseInfo courseInfo = new CourseInfo();
        courseInfo.setCount(counts);
        logger.info("获取课程列表");
        List<CourseInfo> courseInfoList = blogService.getCourseInfoList(courseInfo);
        model.addAttribute("courseInfoList",courseInfoList);
        model.addAttribute("counts",counts);
        return "course";
    }

    @RequestMapping("getCourseInfoById")
    public String getCourseInfo(Integer cid,Model model){
        logger.info("根据课程id获取具体课程");
        CourseInfo courseInfo =  blogService.getCourseInfoById(cid);
        model.addAttribute("courseInfo",courseInfo);
        return "course-details";
    }
    @RequestMapping("getArticle")
    public String getarticleById(Integer aid,Model model,@RequestParam(value = "count",defaultValue = "0") Integer count,HttpSession session){
        logger.info("根据id获取文章");
        BlogUsers user = (BlogUsers) session.getAttribute("user");
        count=count+5;
        if (count==5){
            blogService.addreadtime(aid);
        }
        Article article=blogService.getarticleById(aid);//查文章
        BlogUsers blogUsers=userService.selectUserById(article.getBloguid());//根据文章查作者
        if (blogUsers.getUid()!=user.getUid()){
            long time = new Date().getTime();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd");
            String format = dateFormat.format(time);
            String content = "文章浏览收益";
            blogService.addexrep(blogUsers.getUid(),format,content);
        }
        List<ArticleComment> articleCommentList=blogService.getCommentById(aid,count);   //根据id获取分页评论
        List<Article> likeArticlesList =blogService.getLikeArticles(article.getTypeid(),aid);//获取相关推荐文章
        List<ArticleComment> dateArticlesList=blogService.getdateArticlesList(aid,count);//最新的评论
        Map<Integer,BlogUsers> commentmap=new HashMap<>();//评论人的map
        List<ArticleComment> allComment=blogService.getAllComment();

        for (ArticleComment comment:allComment) {
            BlogUsers blogUsers1=userService.selectUserById(comment.getUserid());
            commentmap.put(comment.getCid(),blogUsers1);
        }
        model.addAttribute("dateArticlesList",dateArticlesList);
        model.addAttribute("aid",aid);
        model.addAttribute("commentmap",commentmap);
        model.addAttribute("article",article);
        model.addAttribute("articleCommentList",articleCommentList);
        model.addAttribute("likeArticlesList",likeArticlesList);
        model.addAttribute("blogUsers",blogUsers);
        model.addAttribute("modle",model);
        model.addAttribute("count",count);
        return "article-details";
    }


    @RequestMapping("insertCoursePerson")
        public String insertCoursePerson(Integer cid, HttpSession session) {
        logger.info("调用用户报名课程的方法");
        session.removeAttribute("massger");
        Date date = new Date();
        BlogUsers user = (BlogUsers) session.getAttribute("user");
        CourseInfo courseInfo = blogService.getCourseInfoById(cid);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = format.format(date);
        int i = date1.compareTo(courseInfo.getBegindate());
        if (i==1){
            session.setAttribute("massger",1);
            return "redirect:/blog/getCourseInfoById?cid="+cid;
        }
        int b = blogService.getPersonCountByCid(cid,user.getUid());
        if (b>0){
            session.setAttribute("massger",0);
            return "redirect:/blog/getCourseInfoById?cid="+cid;
        }

        CoursePerson coursePerson = new CoursePerson(cid,user.getUid(),date1,null,null);
        Boolean flag = blogService.insertCoursePerson(coursePerson);
        return "redirect:/blog/getCourseInfoById?cid="+cid;
    }


    @RequestMapping("/login")
    public String login(BlogUsers blogUsers,HttpSession session){
        logger.info("获取课程列表");
        BlogUsers user = userService.login(blogUsers);
        session.setAttribute("user",user);
        return "redirect:/blog/getIndex";
    }
    @RequestMapping("comment")//回复评论
    @ResponseBody
    public boolean comment(String content,Integer tid,Integer aid,HttpSession session){
        logger.info("回复评论功能");
        BlogUsers user = (BlogUsers)session.getAttribute("user");
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = format.format(date);
        ArticleComment articleComment=new ArticleComment();
        articleComment.setContent(content);
        articleComment.setPid(tid);
        articleComment.setCreatedate(date1);
        articleComment.setStatus("1");
        articleComment.setBlogid(aid);
        articleComment.setUserid(user.getUid());
        logger.info("回复评论");
        int a =articleService.addCommentKid(articleComment);
        return a > 0;
    }
    @RequestMapping("comment2")//评论文章
    public String comment2(String content,Integer aid,HttpSession session){
        logger.info("评论文章功能");
        BlogUsers user = (BlogUsers)session.getAttribute("user");
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = format.format(date);
        ArticleComment articleComment=new ArticleComment();
        articleComment.setContent(content);
        articleComment.setCreatedate(date1);
        articleComment.setStatus("1");
        articleComment.setBlogid(aid);
        articleComment.setUserid(user.getUid());
        logger.info("评论文章");
        int a =articleService.addComment(articleComment);
        return "redirect:/blog/getArticle?aid="+aid;

    }
    @RequestMapping("attention")//关注.
    @ResponseBody
    public boolean attention(Integer uid,HttpSession session,Model model){
        logger.info("关注功能");
        BlogUsers user = (BlogUsers)session.getAttribute("user");
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = format.format(date);
        FocusInfo focusInfo=new FocusInfo();
        focusInfo.setCreatedate(date1);
        focusInfo.setScourceUid(user.getUid());
        focusInfo.setTargetUid(uid);
        int a= 0;
        try {
            a = userService.attention(focusInfo);
        } catch (Exception e) {
            return false;
        }
        return a > 0;
    }
    @RequestMapping("coll")//收藏文章
    @ResponseBody
    public boolean coll(Integer aid,HttpSession session){
        logger.info("文章收藏功能");
        BlogUsers user = (BlogUsers)session.getAttribute("user");
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = format.format(date);
        ArticleCollInfo articleCollInfo=new ArticleCollInfo();
        articleCollInfo.setAtricleid(aid);
        articleCollInfo.setUserid(user.getUid());
        articleCollInfo.setCreatedate(date1);
        ArticleCollInfo collInfo=articleService.getCollInfo(user.getUid(),aid);
        if (collInfo==null){
            articleService.coll(articleCollInfo);
            return true;
        }else {
            articleService.unColl(articleCollInfo);
            return false;
        }

    }


    @RequestMapping("/getArticleList")
    public String getArticleList(Integer counts,Article article,Model model){
        logger.info("调用获取文章列表的方法");
        Integer count1 = counts+5;
        article.setCount(count1);
        List<Article> articleList = blogService.getArticleList(article);
        List<ContentType> contentTypeList = blogService.selectConType();
        List<Article> hotestArticle = blogService.getHotestArticle();
        model.addAttribute("count",count1);
        model.addAttribute("hotestArticle",hotestArticle);
        model.addAttribute("articleList",articleList);
        model.addAttribute("contentTypeList",contentTypeList);
        return "article";
    }
    @RequestMapping("/getcoll")
    public String getcollList(Model model,HttpSession session){
         BlogUsers user = (BlogUsers) session.getAttribute("user");
        List<ArticleCollInfo> collInfoList=blogService.getcollList(user.getUid());
        Map<Integer, Object> collArtMap = new HashMap<>();//收藏的文章
        Map<Integer, Object> collUserMap = new HashMap<>();//收藏文章对应的作者
        for (ArticleCollInfo a:collInfoList){
            Article article = blogService.getarticleById(a.getAtricleid());
            BlogUsers users = userService.selectUserById(article.getBloguid());
            collUserMap.put(a.getAtricleid(),users);
            collArtMap.put(a.getAtricleid(),article);
        }
        model.addAttribute("collInfoList",collInfoList);
        model.addAttribute("collArtMap",collArtMap);
        model.addAttribute("collUserMap",collUserMap);
        return "collect_article";
    }
}
